package com.rxjava.simpleio2;

/**
 * Created by iceGeneral on 2016/10/15.
 */
public final class OperatorMap<T, R> implements Operator<R, T> {

    final Func1<? super T, ? extends R> transformer;

    public OperatorMap(Func1<? super T, ? extends R> transformer) {
    	//1map -> transformer -> return "map" + integer;
    	//2map -> transformer -> return new User(name);
        this.transformer = transformer;
    }

    @Override
    public Subscriber<? super T> call(final Subscriber<? super R> o) {
    	System.out.println("step3");
    	//o -> System.out.println(u);
    	//o -> MapSubscriber对象
        return new MapSubscriber<T, R>(o, transformer);
    }

    private class MapSubscriber<T, R> extends Subscriber<T> {

        private Subscriber<? super R> actual;
        private Func1<? super T, ? extends R> transformer;

        public MapSubscriber(Subscriber<? super R> o, Func1<? super T, ? extends R> transformer) {
        	//actual -> System.out.println(u);
        	//transformer -> return new User(name);
        	//actual -> MapSubscriber对象
        	//transformer -> return "map" + integer;
            this.actual = o;
            this.transformer = transformer;
        }

        @Override
        public void onNext(T t) {
        	System.out.println("step6");
        	System.out.println("t -> " + t);//t=3, t=map3
            R r = transformer.call(t);
            System.out.println("r -> " + r);
            actual.onNext(r);
        }
    }

}
